﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using PPI.Entities.Settings;
using PPI.Entities.XML;
using PPI.General;
using VMind.Global;

namespace PPI.Client.Logic.XML
{
    public class LXmlSettings 
    {
        protected ESettingsBase m_SettingsBase;
        protected XmlDocument m_XmlDocument;

        public LXmlSettings()
        {
            SetupXml();
        }

        private void SetupXml()
        {
            m_XmlDocument = new XmlDocument();
            
            if (File.Exists(PPI.Constants.K_SETTINGSKML))
            {
                try
                {
                    m_XmlDocument.Load(PPI.Constants.K_SETTINGSKML);
                }
                catch (Exception ex)
                {
                    CreateBaseSchema();
                }

            }
            if (m_XmlDocument.ChildNodes.Count == 0)
            {
                CreateBaseSchema();
            }
            m_XmlDocument.Save(PPI.Constants.K_SETTINGSKML);


        }

        private void CreateBaseSchema()
        {
            XmlNode _SettingsNode;
            XmlNode _DocumentNode = m_XmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null);
            m_XmlDocument.AppendChild(_DocumentNode);

            _SettingsNode = m_XmlDocument.CreateElement("Settings");
            m_XmlDocument.AppendChild(_SettingsNode);
        }

        private XmlNode GetParentSettingsNode()
        {
            return m_XmlDocument.ChildNodes[1];
        }

        public ESettingsBase GetSetting(ESettingsBase settingsBase)
        {
            string _xmlQuery = string.Format(@"//Setting[@Key='{0}']", settingsBase.SettingKey);

            XmlNode settingsNode = m_XmlDocument.SelectSingleNode(_xmlQuery);
            if(settingsNode == null)
                return null;

            ESettingsBase returnSetting = settingsBase;
            returnSetting.SettingId = ConvertHelper.ToInt32(settingsNode["Id"].InnerText);
            returnSetting.SettingText = settingsNode["Text"].InnerText;
            returnSetting.SettingValue = settingsNode["Value"].InnerText;

            return returnSetting;
        }

        public void UpdateNode(ESettingsBase settingsBase)
        {
            string _xmlQuery = string.Format(@"//Setting[@Key='{0}']", settingsBase.SettingKey);
            XmlNode settingNode = m_XmlDocument.SelectSingleNode(_xmlQuery);
            settingNode["Id"].InnerText = settingsBase.SettingId.ToString();
            settingNode["Text"].InnerText = settingsBase.SettingText;
            settingNode["Value"].InnerText = settingsBase.SettingValue;
            m_XmlDocument.Save(PPI.Constants.K_SETTINGSKML);
        }

        public void InsertNode(ESettingsBase settingsBase)
        {
            XmlNode parentSettingNode = GetParentSettingsNode();
            parentSettingNode.AppendChild(settingsBase.CreateNode(m_XmlDocument));
            m_XmlDocument.Save(PPI.Constants.K_SETTINGSKML);

        }

    }
}
